#!/usr/bin/env python3
from scapy.all import sniff, TCP, UDP, IP, get_if_list

def get_eth0():
    for iface in get_if_list():
        if "eth0" in iface:
            return iface
    print("Error: eth0 not found")
    exit(1)

def handle_pkt(pkt):
    if IP in pkt:
        src = pkt[IP].src
        dst = pkt[IP].dst
        if TCP in pkt:
            print(f"\n[TCP] {src}:{pkt[TCP].sport} → {dst}:{pkt[TCP].dport}")
            print(f"Content: {bytes(pkt[TCP].payload).decode(errors='ignore')}")
        elif UDP in pkt:
            print(f"\n[UDP] {src}:{pkt[UDP].sport} → {dst}:{pkt[UDP].dport}")
            print(f"Content: {bytes(pkt[UDP].payload).decode(errors='ignore')}")
        print("-" * 60)

def main():
    iface = get_eth0()
    print(f"Listening for TCP/UDP packets on {iface}...")
    sniff(iface=iface, filter="tcp or udp", prn=handle_pkt, store=0)

if __name__ == '__main__':
    main()
